package com.baofu.paycore.service.processor.transfer;

import com.baofu.paycore.common.enums.FlagEnum;
import com.baofu.paycore.common.enums.FundTypeEnum;
import com.baofu.paycore.common.enums.UpdateUser;
import com.baofu.paycore.common.exception.ManagerErrorCode;
import com.baofu.paycore.dal.model.SubTransferDO;
import com.baofu.paycore.manager.model.TransferBO;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.baofu.paycore.service.facade.enums.PayStatusEnums;
import com.baofu.paycore.service.facade.model.transfer.CancelTransferDTO;
import com.baofu.paycore.service.processor.PayBaseProcessor;
import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import com.system.commons.exception.BizServiceException;
import com.system.commons.sequence.redis.SequenceFacade;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 转账资金单处理类
 *
 * <p/>
 *      1、构建支付属性
 * </p>
 * User: LZQ Date: 2016/04/20  Version: 1.0
 */
@Slf4j
@Component
public class CancelTransferFundProcessor implements PayBaseProcessor<TransferBO,CancelTransferDTO> {

    /**
     * 资金单明细NO
     */
    @Autowired
    private SequenceFacade sequenceFacade;

    /**
     * 支付内部对象构建
     *
     * @param transferBO    支付模型
     * @param payBaseDTO    请求参数
     */
    @Override
    public PayBaseProcessor process(TransferBO transferBO, CancelTransferDTO payBaseDTO) {
        if(Objects.equal(FlagEnum.TRUE.getCode(),payBaseDTO.getRefundFeeFlag())){
            log.error("转账撤销暂不支持退手续费:{}",payBaseDTO.getRequestBizNo());
            throw new BizServiceException(ManagerErrorCode.REFUND_FEE_NOT_SUPPORT);
        }
        List<SubTransferDO> subTransferDOs = transferBO.getOldTransferBO().getSubTransferDO();
        SubTransferDO oldSubTransferDO = null;
        for(SubTransferDO  subTransferDO:subTransferDOs){
            if(Objects.equal(FundTypeEnum.TRANSFER.getCode(),subTransferDO.getFundType())){
                oldSubTransferDO = subTransferDO;
                break;
            }
        }
        if(oldSubTransferDO == null){
            log.error("转账资金单不存在:{}",payBaseDTO.getRequestBizNo());
            throw new BizServiceException(PayCoreErrorCode.ORIG_ORDER_NOT_EXIST);
        }
        transferBO.setSubTransferDO(Lists.newArrayList(build(oldSubTransferDO,transferBO.getPayNo())));
        return this;
    }

    /**
     * 组装转账撤销资金单
     *
     * @param oldSubTransferDO         原资金单
     * @param payNo                    支付单号
     * @return                          新资金单
     */
    private SubTransferDO build(SubTransferDO oldSubTransferDO,String payNo){
        SubTransferDO subTransferDO = new SubTransferDO();
        subTransferDO.setFundDetailNo(sequenceFacade.getUniqueSeq());
        subTransferDO.setPayNo(payNo);
        subTransferDO.setInAccountNo(oldSubTransferDO.getOutAccountNo());
        subTransferDO.setInContractNo(oldSubTransferDO.getOutContractNo());
        subTransferDO.setOutAccountNo(oldSubTransferDO.getInAccountNo());
        subTransferDO.setOutContractNo(oldSubTransferDO.getInContractNo());
        subTransferDO.setPayAmount(oldSubTransferDO.getPayAmount());
        subTransferDO.setCcy(oldSubTransferDO.getCcy());
        subTransferDO.setStatus(PayStatusEnums.INIT.getCode());
        subTransferDO.setCreatedBy(UpdateUser.SYS.getUserName());
        subTransferDO.setFundType(oldSubTransferDO.getFundType());
        return subTransferDO;
    }
}
